package com.seari.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seari.bean.ArrangementPostBean;
import com.seari.bean.ArrangementPostMessageBean;
import com.seari.bean.ArrangementPostModelBean;
import com.seari.kafka.TransferWorker;
import com.seari.mapper.ArrangementPostMapper;

@Service
public class ArrangementPostServiceImpl implements ArrangementPostService
{

	@Autowired
	ArrangementPostMapper arrangementPostMapper;

	@Override
	public List<ArrangementPostBean> renderArrangementPostInfo(String start)
	{
		List<ArrangementPostBean> renderPostEvent = arrangementPostMapper.renderPostEvent(start);
		StringBuffer bf = new StringBuffer("");
		for (ArrangementPostBean bean : renderPostEvent)
		{
			
			bf.append(bean.getStart()).append("-").append(bean.getEnd()).append("     (")
					.append(bean.getPostId()).append(")     ").append("区域：").append(bean.getArea()).append("   工作：").append(bean.getItem());
			bean.setTitle(bf.toString());
			bf.setLength(0);
		}
		return renderPostEvent;

	}

	@Override
	public int insertArrangementPostInfo(ArrangementPostBean bean)
	{
		return arrangementPostMapper.insertPostEvent(bean);
	}

	@Override
	public List<Integer> checkPostEventExists(Integer resourceId, String start, String end)
	{
		return arrangementPostMapper.checkPostEventExists(resourceId, start, end);
	}

	@Override
	public int deletePostEvent(int pid)
	{
		return arrangementPostMapper.deletePostEvent(pid);
	}

	@Override
	public int updatePostEvent(ArrangementPostBean bean)
	{
		return arrangementPostMapper.updatePostEvent(bean);
	}

	@Override
	public List<ArrangementPostBean> renderArrangementPostInfos()
	{
		return arrangementPostMapper.renderPostEvents();
	}

	@Override
	public List<ArrangementPostBean> renderModelPostEventHis(String date, String modelName)
	{
		List<ArrangementPostBean> renderModelPostEvent = arrangementPostMapper.renderModelPostEventHis(date, modelName);
		
		StringBuffer bf = new StringBuffer("");
		for (ArrangementPostBean bean : renderModelPostEvent)
		{
			bf.append(bean.getStart()).append("-").append(bean.getEnd()).append("     (")
					.append(bean.getPostId()).append(")     ").append("区域：").append(bean.getArea()).append("   工作：").append(bean.getItem());
			bean.setTitle(bf.toString());
			bf.setLength(0);
		}
		return renderModelPostEvent;
	}

	@Override
	public List<ArrangementPostModelBean> fetchPostModel()
	{
		return arrangementPostMapper.fetchPostModel();
	}

	@Override
	public List<ArrangementPostMessageBean> fecthMessageBean(String modelName)
	{
		return arrangementPostMapper.fecthMessageBean(modelName);
	}

	@Override
	public void confirmPostModel(String modelId)
	{
		List<ArrangementPostMessageBean> messageBean = arrangementPostMapper.fecthMessageBean(modelId);
		TransferWorker worker = new TransferWorker();
		worker.setTopic("post_info");
		worker.setData(messageBean);
		worker.setType("布岗信息");
		new Thread(worker).start();
		System.out.println(messageBean);
	}

	@Override
	public List<ArrangementPostBean> renderModelPostEvent(String modelName)
	{
		List<ArrangementPostBean> postInfo = null;
		if(modelName.equals("")) {
			postInfo = arrangementPostMapper.renderModelPostEventWithNDate();
		}else {
			postInfo = arrangementPostMapper.renderModelPostEvent(modelName);
		}
		return renderEventTitle(postInfo);
	}
	
	@SuppressWarnings("unused")
	private static List<ArrangementPostBean> renderEventTitle(List<ArrangementPostBean> renderModelPostEvent) {
		
		StringBuffer bf = new StringBuffer("");
		for (ArrangementPostBean bean : renderModelPostEvent)
		{
			bf.append(bean.getStart()).append("-").append(bean.getEnd()).append("     (")
					.append(bean.getPostId()).append(")     ").append("区域：").append(bean.getArea()).append("   工作：").append(bean.getItem());
			bean.setTitle(bf.toString());
			bf.setLength(0);
		}
		return renderModelPostEvent;
	}

}
